package framework.jiekou.sub;

import framework.http.bean.Image;
import framework.http.net.ImageLoader;
import framework.http.net.ImageLoader.ImageCallback;
import framework.jiekou.ViewSetting;
import framework.manage.ManageApp;
import framework.utils.LogUtils;
import framework.utils.StringUtils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewSettingImpl implements ViewSetting {

	private Context context;

	public ViewSettingImpl(Context context) {
		this.context = context;
	}

	@Override
	public Context getContext() {
		return this.context;
	}

	@Override
	public void setContext(Context context) {
		this.context = context;
	}

	@Override
	public View inflate(int resourceId) {
		View inflate = LayoutInflater.from(getContext()).inflate(resourceId, null);
		if (inflate instanceof ViewGroup) {
			layoutSetting((ViewGroup) inflate);
		} else {
			throw new RuntimeException("这个布局不是ViewGroup类型的");
		}
		return inflate;
	}

	@Override
	public void layoutSetting(ViewGroup ll) {
		for (int i = 0; i < ll.getChildCount(); i++) {
			View child = ll.getChildAt(i);

			if (child instanceof ViewGroup) {
				layoutSetting((ViewGroup) child);
			}

			MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
			if (layoutParams.width > 0) {
				layoutParams.width = layoutAdjustSize(layoutParams.width);
			}
			if (layoutParams.height > 0) {
				layoutParams.height = layoutAdjustSize(layoutParams.height);
			}

			layoutParams.topMargin = layoutAdjustSize(layoutParams.topMargin);
			layoutParams.leftMargin = layoutAdjustSize(layoutParams.leftMargin);
			layoutParams.rightMargin = layoutAdjustSize(layoutParams.rightMargin);
			layoutParams.bottomMargin = layoutAdjustSize(layoutParams.bottomMargin);

			if (child instanceof TextView) {
				TextView tv = (TextView) child;
				tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, layoutAdjustSize(tv.getTextSize()));
				tv.setPadding(layoutAdjustSize(tv.getPaddingLeft()), layoutAdjustSize(tv.getPaddingTop()), layoutAdjustSize(tv.getPaddingRight()), layoutAdjustSize(tv.getPaddingBottom()));

			}

			child.setLayoutParams(layoutParams);
		}
	}

	@Override
	public void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

	@Override
	public void adjustTextView(TextView tv) {
		measureView(tv);
		Float textSize = tv.getTextSize();
		int cha = (int) ((tv.getMeasuredHeight() - textSize) / 2);

		ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) tv.getLayoutParams();
		layoutParams.topMargin = layoutParams.topMargin - cha;
		tv.setLayoutParams(layoutParams);
	}

	@Override
	public void setImageView(final ImageView imageView, String url, final int defaultId) {
		if (!TextUtils.isEmpty(url)) {
			Drawable loadDrawable = new ImageLoader().loadDrawable(getContext(), new Image(url), new ImageCallback() {

				@Override
				public void imageLoaded(Drawable loadDrawable, String imageUrl) {
					if (loadDrawable != null) {
						imageView.setBackgroundDrawable(loadDrawable);
					} else {
						imageView.setBackgroundResource(defaultId);
					}
				}
			});

			if (loadDrawable != null) {
				imageView.setBackgroundDrawable(loadDrawable);
			} else {
				imageView.setBackgroundResource(defaultId);
			}

		} else {
			imageView.setBackgroundResource(defaultId);
		}
	}

	@Override
	public int layoutAdjustSize(int getW) {
		return (int) ((getW * getScreenWidth()) / (getDesignWidth() * getDensity()));
	}

	@Override
	public Float layoutAdjustSize(Float getW) {
		return (getW * getScreenWidth()) / (getDesignWidth() * getDensity());
	}

	@Override
	public int adjustSize(int ori) {
		return ori * getScreenWidth() / getDesignWidth();
	}

	@Override
	public int getScreenWidth() {
		return ManageApp.getSreenWidth(getContext());
	}

	@Override
	public int getScreenHight() {
		return ManageApp.getScreenHight(getContext());
	}

	@Override
	public int getDesignWidth() {
		return ManageApp.getDesignWidth();
	}

	@Override
	public float getDensity() {
		return ManageApp.getDensity(getContext());
	}

	public void viewShowOrHidden(View view) {
		viewVisible(view, !isVisible(view));
	}

	public boolean isVisible(View view) {
		return view.getVisibility() == View.VISIBLE;
	}

	public void viewVisible(View view, int id, boolean isVisible) {
		viewVisible(view.findViewById(id), isVisible);
	}

	public void viewVisible(View view, boolean isVisible) {
		view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
	}

	public void viewShow(int id, View itemView) {
		viewShow(itemView.findViewById(id));
	}

	public void viewShow(View view) {
		viewVisible(view, true);
	}

	public void viewGone(int id, View itemView) {
		viewGone(itemView.findViewById(id));
	}

	public void viewGone(View view) {
		viewVisible(view, false);
	}

	@Override
	public LinearLayout findLinearLayoutById(int id, View view) {
		return (LinearLayout) view.findViewById(id);
	}

	@Override
	public ListView findListViewById(int id, View view) {
		return (ListView) view.findViewById(id);
	}

	@Override
	public Button findButtonById(int id, View view) {
		return (Button) view.findViewById(id);
	}

	@Override
	public ProgressBar findProgressBarById(int id, View view) {
		return (ProgressBar) view.findViewById(id);
	}

	@Override
	public ImageView findImageViewById(int id, View view) {
		return (ImageView) view.findViewById(id);
	}

	@Override
	public RelativeLayout findRelativeLayoutById(int id, View view) {
		return (RelativeLayout) view.findViewById(id);
	}

	@Override
	public TextView findTextViewById(int id, View view) {
		return (TextView) view.findViewById(id);
	}

	@Override
	public CheckBox findCheckBoxById(int id, View view) {
		return (CheckBox) view.findViewById(id);
	}

	@Override
	public FrameLayout findFrameLayoutById(int id, View view) {
		return (FrameLayout) view.findViewById(id);
	}

	@Override
	public ImageButton findImageButtonById(int id, View view) {
		return (ImageButton) view.findViewById(id);
	}

	@Override
	public EditText findEditTextById(int id, View view) {
		return (EditText) view.findViewById(id);
	}

	@Override
	public void log(String msg) {
		LogUtils.log(msg);
	}

	@Override
	public void log(Object msg) {
		LogUtils.log(msg);
	}

	@Override
	public void isNull(String name, Object obj) {
		log(name + "==null:" + (obj == null));
	}

	@Override
	public void exception(Exception e) {
		LogUtils.exception(e);
	}

	@Override
	public void setText(TextView tv, String str) {
		if (TextUtils.isEmpty(str)) {
			tv.setText("");
		} else {
			tv.setText(str);
		}
	}

	@Override
	public void setText(TextView tv, Object obj) {
		setText(tv, String.valueOf(obj));
	}

}
